package com.train.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.train.common.enums.impl.BizExceptionEnums;
import com.train.common.exception.GlobalException;
import com.train.common.resp.PageResp;
import com.train.system.dto.seat.TrainSeatDTO;
import com.train.system.dto.seat.TrainSeatPageDTO;
import com.train.system.mapper.TrainSeatMapper;
import com.train.system.po.TrainSeatPO;
import com.train.system.resp.seat.TrainSeatPageResp;
import com.train.system.service.TrainSeatService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【trainSeat(座位)】的数据库操作Service实现
 * @createDate 2025-02-20 23:20:20
 */
@Service
@RequiredArgsConstructor
public class TrainSeatServiceImpl extends ServiceImpl<TrainSeatMapper, TrainSeatPO>
    implements TrainSeatService {

    private final TrainSeatMapper trainSeatMapper;

    @Override
    public PageResp<TrainSeatPageResp> pageList(TrainSeatPageDTO trainSeatPageDTO) {
        return trainSeatMapper.pageList(trainSeatPageDTO);
    }

    @Override
    public void remove(Long id) {
        boolean isRemoved = this.removeById(id);
        if (!isRemoved) {
            throw new GlobalException(BizExceptionEnums.EXP_REMOVE_TRAIN_SEAT);
        }
    }

    @Override
    public void removeSeatByTrainCode(String trainCode) {
        boolean removed = trainSeatMapper.removeSeatByTrainCode(trainCode);
        if (!removed) {
            throw new GlobalException(BizExceptionEnums.EXP_REMOVE_TRAIN_SEAT);
        }
    }

    @Override
    public void saveBatchTrainSeat(List<TrainSeatDTO> trainSeatDTOList) {
        if (CollectionUtil.isEmpty(trainSeatDTOList)) {
            throw new GlobalException(BizExceptionEnums.EXP_TRAIN_SEAT_GENERATE);
        }
        List<TrainSeatPO> trainSeatPOS = BeanUtil.copyToList(trainSeatDTOList, TrainSeatPO.class);
        boolean saved = this.saveBatch(trainSeatPOS);
        if (!saved) {
            throw new GlobalException(BizExceptionEnums.EXP_BATCH_ADD_TRAIN_SEAT);
        }
    }

    @Override
    public boolean existsSeatByTrainCode(String trainCode) {
        return trainSeatMapper.existsSeatByTrainCode(trainCode);
    }

    @Override
    public List<TrainSeatPageResp> getTrainSeatListByTrainCodes(List<String> trainCodes) {
        return trainSeatMapper.getTrainSeatListByTrainCodes(trainCodes);
    }
}




